package com.glaciervirtualdevice.controller;

import static com.glaciervirtualdevice.constant.DataConstant.*;

import com.glaciervirtualdevice.dto.THDateDTO;
import com.glaciervirtualdevice.entity.VirtualDevice;
import com.glaciervirtualdevice.service.VitrualDeviceService;
import com.glaciervirtualdevice.utils.Result;


import com.glaciervirtualdevice.utils.StateUtils;
import com.glaciervirtualdevice.utils.THDate;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Sun
 * @DateTime: 2025/4/1 16:45
 * @Description:设备control
 **/
@Slf4j
@Api(tags = "虚拟设备接口")
@RestController
@RequestMapping("/device")
public class VitrualDeviceController {
    @Autowired
    private VitrualDeviceService vitrualDeviceService;

    /**
     * 创建一个仪表硬件
     * @return
     */

    @PostMapping("/createDevice")
    @ApiOperation("创建仪表硬件接口")
    public Result<Boolean> createDevice() {
        return vitrualDeviceService.createDevice() ? Result.success(ADD_YES_MSG) : Result.success(ADD_NO_MSG);
    }

    /**
     * 检查当前设备的状态
     *
     * @param sn
     * @param token
     * @return
     */
    @PostMapping("/checkDevice")
    @ApiOperation("检查设备接口")
    public Result checkDevice(@RequestParam String sn, @RequestParam String token) {
        StateUtils stateUtils = new StateUtils();
        stateUtils.setState(vitrualDeviceService.checkDevice(sn, token));
        return Result.success(stateUtils);
    }

    /**
     * 删除设备
     *
     * @param sn
     * @param token
     * @return
     */
    @PostMapping("/deleteDevice")
    @ApiOperation("删除设备接口")
    public Result<Void> deleteDevice(@RequestParam String sn, @RequestParam String token) {
        return vitrualDeviceService.deleteDevice(sn, token)? Result.success(DEL_YES_MSG) : Result.success(DEL_NO_MSG);
    }

    /**
     * 查询对应仪表的温度
     * @param sn
     * @param token
     * @return
     */

    @PostMapping("/getDeviceInfo")
    @ApiOperation("查询对应的设备温湿度仪表")
    public Result getDeviceInfo(@RequestParam String sn, @RequestParam String token) {
        if (vitrualDeviceService.checkDevice(sn, token) != 0){
            return Result.err("该设备正在维修---已停用");
        }
        THDateDTO deviceInfo = vitrualDeviceService.getDeviceInfo(sn, token);
        if (deviceInfo == null){
            Result.err("数据出现错误");
        }
        THDate thDate = new THDate();
        thDate.setDate(deviceInfo);
        thDate.setSn(sn);

        return Result.success(thDate);
    }

    /**
     * 查询所有仪表硬件
     * @return
     */
    @GetMapping("/viewAllDevice")
    @ApiOperation("查询所有仪表硬件")
    public Result<List<VirtualDevice>> viewAllDevice() {
        return Result.success(vitrualDeviceService.viewAllDevice());
    }

}
